﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using WineStore.Models;
using System.Data;
using System.Web.Security;

namespace WineStore.Controllers
{
    public class AdminController : Controller
    {

        // GET: /AccountAdmin/LogOn/
        public ActionResult LogOn()
        {
            return View();
        }

        // POST: /AccountAdmin/LogOn/
        [HttpPost]
        public ActionResult LogOn(AccountAdmin account)
        {
            if (ModelState.IsValid)
            {
                if (ValidateUser(account.UserName, account.Password))
                {
                    FormsAuthentication.SetAuthCookie(account.UserName, false);
                    Session["Admin"] = account.UserName;
                    Session.Timeout = 2880;
                    return RedirectToAction("ShowSim", "Sim");
                }
                else
                {
                    ModelState.AddModelError("", "Bạn đã nhập sai username hoặc password !");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(account);
        }

        public static bool ValidateUser(string username, string password)
        {
            string encodePassword = EncodePassword(password);
            using (var db = new WineDbContext())
            {
                foreach (AccountAdmin account in db.AccountAdmins)
                {
                    if (account.UserName.Equals(username) && account.Password.Equals(encodePassword))
                        return true;
                }
            }
            return false;
        }

        // GET: /LogOff

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("LogOn", "Admin");
        }

        // GET: /AccountAdmin/ShowAccount/
        public ViewResult ShowAccount()
        {
            using (var db = new WineDbContext())
            {
                var accounts = db.AccountAdmins.ToList();
                return View(accounts);
            }
        }


        // GET: /AccountAdmin/EditAccount/
        [HttpGet]
        public ActionResult EditAccount(int id = 0)
        {
            AccountAdmin account = null;
            using (var db = new WineDbContext())
            {
                account = db.AccountAdmins.Find(id);
                if (account == null)
                {
                    return HttpNotFound();
                }
            }
            return View(account);
        }

        // POST: /AccountAdmin/EditAccount/
        [HttpPost]
        public ActionResult EditAccount(AccountAdmin account)
        {
            if (account.Password != null)
            {
                account.Password = EncodePassword(account.Password);
            }
            try
            {
                using (var db = new WineDbContext())
                {
                    if (ModelState.IsValidField(account.UserName))
                    {
                        db.Entry(account).State = EntityState.Modified;
                        db.SaveChanges();

                        return RedirectToAction("ShowAccount", "Admin");
                    }
                }

            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(account);
        }

        //Method to hash parameters to generate the Reset URL
        public static string EncodePassword(string password)
        {
            byte[] bytesofLink = System.Text.Encoding.UTF8.GetBytes(password);
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            string HashParams = BitConverter.ToString(md5.ComputeHash(bytesofLink));

            return HashParams;
        }

    }
}

